package org.hyjiacan.deadqq.common.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MoveWindow implements MouseListener, MouseMotionListener,
		ActionListener {
	Point oldWindowPos;
	Point oldMousePos;
	JFrame f = new JFrame("Moveable window demo");
	public static final int WIDTH = 320;
	public static final int HEIGHT = 480;
	JButton min = new JButton();
	JButton max = new JButton();
	JButton close = new JButton();
	JLabel title = new JLabel("title");
	boolean hideByEdge = false;

	public MoveWindow() {
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(WIDTH, HEIGHT);

		WindowUtil.center(f);

		f.addMouseListener(this);
		f.addMouseMotionListener(this);

		f.setLayout(null);

		title.setBounds(0, 0, f.getWidth() - 72, 24);
		f.add(title);

		min.setBounds(f.getWidth() - 72, 0, 24, 24);
		min.addActionListener(this);
		min.setBorder(null);
		min.setToolTipText("Minimum");
		min.addMouseListener(this);
		f.add(min);

		max.setBounds(f.getWidth() - 48, 0, 24, 24);
		max.addActionListener(this);
		max.setBorder(null);
		max.setToolTipText("Maximum");
		max.addMouseListener(this);
		f.add(max);

		close.setBounds(f.getWidth() - 24, 0, 24, 24);
		close.addActionListener(this);
		close.setBorder(null);
		close.setToolTipText("Close");
		close.addMouseListener(this);
		f.add(close);
		f.setUndecorated(true);
		f.setVisible(true);
		f.validate();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		MoveWindow mw = new MoveWindow();
		mw.setTitle("Fuck window");
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

		if (arg0.getSource() == min || arg0.getSource() == max
				|| arg0.getSource() == close) {
		} else {
			Point m = arg0.getLocationOnScreen();
			int x = m.x - oldMousePos.x;
			int y = m.y - oldMousePos.y;
			f.setLocation(new Point(oldWindowPos.x + x, oldWindowPos.y + y));
		}
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource() == min || arg0.getSource() == max
				|| arg0.getSource() == close) {
			((JButton) arg0.getSource()).setBackground(Color.CYAN);
		}
		if (hideByEdge) {
			f.setSize(WIDTH, HEIGHT);
			f.validate();
			hideByEdge=false;
		}
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource() == min || arg0.getSource() == max
				|| arg0.getSource() == close) {
			((JButton) arg0.getSource()).setBackground(null);
		} else {
			if (f.getY() < 1) {
				f.setLocation(f.getX(), 0);
				int h = f.getHeight();
				while (h > 1) {
					h = (int) (h * 0.9999);
					f.setSize(WIDTH, h);
				}
				f.setSize(WIDTH, 1);
				f.setAlwaysOnTop(true);
				f.validate();
				hideByEdge = true;
			} else {
				f.setSize(WIDTH, HEIGHT);
				f.setAlwaysOnTop(false);
				f.validate();
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource() == min || arg0.getSource() == max
				|| arg0.getSource() == close) {
		} else {
			oldMousePos = arg0.getLocationOnScreen();
			oldWindowPos = f.getLocationOnScreen();
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == min) {

		} else if (e.getSource() == max) {
			f.setMaximumSize(new Dimension(640, 320));
			f.setSize(f.getMaximumSize());
		} else if (e.getSource() == close) {

			System.exit(0);
		}
	}

	public void setTitle(String title) {
		this.title.setText(title);
	}
}
